<?php
/**
 * 表单组件处理
 * User: admin
 * Date: 2018/11/27
 * Time: 9:47
 */
namespace plugins\qbt_custom_model\service;
class HtmlService{

    /**
     * 处理分发
     */
    public function get_html($type,$field_info){
        return self::$type($field_info);
    }

    /**
     * 单行文本
     */
    private function text($field_info){
        $html = '<tr>
                    <th width="10%">'.$field_info['field_nikename'];
        if($field_info['field_is_required']){
            $html .='<span class="form-required">*</span>';
        }
        $html .= '</th>
                    <td>
                        <input class="form-control" type="text" name="'.$field_info['field_name'].'"
                               id="title" ';
        if($field_info['field_is_required']){
            $html .= ' required ';
        }
        $html .= ' value="" placeholder="'.$field_info['field_nikename'].'"/>';
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .= '</td>
                </tr>';
        return $html;
    }

    /**
     * 多行文本
     */
    private function textarea($field_info){
        $html = ' <tr>
        <th width="10%">'.$field_info['field_nikename'];

        if($field_info['field_is_required']){
            $html .='<span class="form-required">*</span>';
        }
        $html .= '</th>
        <td>
         <textarea class="form-control" name="'.$field_info['field_name'].'" style="height: 50px;"
                                      placeholder="请填写'.$field_info['field_nikename'].'"></textarea>';
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .= '</td>
        </tr>';
        return $html;
    }

    /**
     * 富文本
     */
    private function html($field_info){
        $html = '
            <tr>
                <th width="10%">'.$field_info['field_nikename'].'</th>
                <td>
                    <script type="text/plain" id="'.$field_info['field_name'].'" name="'.$field_info['field_name'].'"  style="height:300px;"></script>
                </td>
            </tr>
            <script type="text/javascript">
                $(function() {
                    editorcontent = new baidu.editor.ui.Editor();
                    editorcontent.render(\''.$field_info['field_name'].'\');
                    try {
                        editorcontent.sync();
                    } catch (err) {
                    }
                })
            </script>
        ';
        return $html;
    }

    /**
     * 单选
     */
    private function radio($field_info){
        $html = '<tr>
            <th width="10%">'.$field_info['field_nikename'].'</th>
            <td>';
        //循环取出按钮
        $btn = preg_split('/[;\r\n]+/s', $field_info['field_choose']);
        foreach ($btn as $k=>$v){
            $field_choose = explode('=',$v);
            $html .= '<label>
        <input type="radio" id="post-status-radio" name="'.$field_info['field_name'].'" value="'.$field_choose[0].'" ';
            if($k==$field_info['field_default']){
                $html .= ' checked=""';
            }
            $html .= '>'.$field_choose[1].'
        </label>&nbsp;&nbsp;&nbsp;&nbsp;';
        }
        $html .= '
            </td>
        </tr>';

        return $html;
    }

    /**
     * 多选
     */
    private function checkbox($field_info){
        $html = '<tr><th width="10%">'.$field_info['field_nikename'].'</th><td>';
        //循环取出按钮
//        $btn = explode(',',$field_info['field_choose']);
        $btn = preg_split('/[;\r\n]+/s', $field_info['field_choose']);
        foreach ($btn as $k=>$v){
            $field_choose = explode('=',$v);
            $html .= '<label>
        <input type="checkbox" id="post-status-checkbox" name="'.$field_info['field_name'].'[]" value="'.$field_choose[0].'">'.$field_choose[1].'
        </label>&nbsp;&nbsp;&nbsp;&nbsp;';
        }
        return $html;
    }

    /**
     * 下拉
     */
    private function select($field_info){
         if(!$field_info['field_choose']) return '';
        $html = '<tr><th width="10%">'.$field_info['field_nikename'].'</th><td><select name="'.$field_info['field_name'].'" class="form-control">';

        //循环取出按钮
//        $btn = explode(',',$field_info['field_choose']);
        $btn = preg_split('/[;\r\n]+/s', $field_info['field_choose']);
        //$html .= '<option value="0">请选择</option>';
        foreach ($btn as $k=>$v){
            $field_choose = explode('=',$v);
            $html .= '<option value="'.$field_choose[0].'" ';
            if($k==$field_info['field_default']){
                $html .= 'selected=""';
            }
            $html .= '>'.$field_choose[1].'</option>';
        }
        $html .="</select>";
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .="</td></tr>";
        return $html;

    }

    /**
     * 图片
     */
    private function image($field_info){
        $html = '<tr><th width="10%">'.$field_info['field_nikename'].'</th><td>';
        $html .= '<div style="text-align: left;">
                    <input type="hidden" name="'.$field_info['field_name'].'" id="'.$field_info['field_name'].'" value="">
                    <a href="javascript:uploadOneImage(\'图片上传\',\'#'.$field_info['field_name'].'\');">
                        <img src="__TMPL__/public/assets/images/default-thumbnail.png"
                             id="'.$field_info['field_name'].'-preview"
                             width="135" style="cursor: pointer"/>
                    </a>
                    <input type="button" class="btn btn-sm"
                                       onclick="$(\'#'.$field_info['field_name'].'-preview\').attr(\'src\',\'__TMPL__/public/assets/images/default-thumbnail.png\');$(\'#'.$field_info['field_name'].'\').val(\'\');return false;"
                                       value="取消图片">
                </div>';
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .="</td></tr>";

        return $html;
    }

    /**
     * 多图片
     */
    private function images($field_info){
        $html = '<tr><th width="10%">'.$field_info['field_nikename'].'</th>';
        $html .= '<td>
                        <ul id="'.$field_info['field_name'].'" class="pic-list list-unstyled form-inline"></ul>
                        <a href="javascript:uploadMultiImage(\'图片上传\',\'#'.$field_info['field_name'].'\',\''.$field_info['field_name'].'-item-tpl\');"
                           class="btn btn-default btn-sm">选择图片</a>';
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .="</td></tr>";

        return $html;
    }

    /**
     * 附件
     */
    private function file($field_info){
        $html = '<tr>
        <th width="10%">'.$field_info['field_nikename'].'</th>
        <td class="form-inline">
        <input id="file-'.$field_info['field_name'].'" class="form-control" type="text" name="'.$field_info['field_name'].'"
        value="{$post.'.$field_info['field_name'].'|default=\'\'}" placeholder="请上传'.$field_info['field_nikename'].'" style="width: 200px;">
        <notempty name="post.'.$field_info['field_name'].'">
        <a id="file-'.$field_info['field_name'].'-preview" href="{:cmf_get_file_download_url($post.'.$field_info['field_name'].')}"
        target="_blank">下载</a>
        </notempty>

        <a href="javascript:uploadOne(\'文件上传\',\'#file-'.$field_info['field_name'].'\',\''.$field_info['field_file_type'].'\');">上传</a>
        </td>
        </tr>';
        return $html;
    }

    /**
     * 多附件
     */
    private function files($field_info){
        $html = '<tr>
                        <th width="10%">'.$field_info['field_nikename'].'</th>
                        <td>
                            <ul id="'.$field_info['field_name'].'" class="pic-list list-unstyled form-inline">
                            </ul>
                            <a href="javascript:uploadMultiFile(\'附件上传\',\'#'.$field_info['field_name'].'\',\''.$field_info['field_name'].'-item-tpl\',\''.$field_info['field_file_type'].'\');"
                               class="btn btn-sm btn-default">选择文件</a>
                        </td>
                    </tr>';
        return $html;
    }

    /**
     * 整数
     */
    private function number($field_info){
        $html = '<tr>
                    <th width="10%">'.$field_info['field_nikename'];
        if($field_info['field_is_required']){
            $html .='<span class="form-required">*</span>';
        }
        $html .= '</th>
                    <td>
                        <input class="form-control" type="text" name="'.$field_info['field_name'].'"
                               id="title" ';
        if($field_info['field_is_required']){
            $html .= ' required ';
        }
        $html .= ' value="" placeholder="'.$field_info['field_nikename'].'"/>';
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .= '</td>
                </tr>';
        return $html;
    }

    /**
     * 小数
     */
    private function float($field_info){
        $html = '<tr>
                    <th width="10%">'.$field_info['field_nikename'];
        if($field_info['field_is_required']){
            $html .='<span class="form-required">*</span>';
        }
        $html .= '</th>
                    <td>
                        <input class="form-control" type="text" name="'.$field_info['field_name'].'"
                               id="title" ';
        if($field_info['field_is_required']){
            $html .= ' required ';
        }
        $html .= ' value="" placeholder="'.$field_info['field_nikename'].'"/>';
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .= '</td>
                </tr>';
        return $html;
    }

    /**
     * 时间
     */
    private function datetime($field_info){
        $html = '<tr><th width="10%">'.$field_info['field_nikename'].'</th><td>';
        $html .= '<input class="form-control js-bootstrap-datetime" type="text" name="'.$field_info['field_name'].'"
                                   value="{:date(\'Y-m-d H:i:s\',time())}">
                        </td>
                    </tr>';
        return $html;
    }

    /**
     * 外联
     */
    private function outreach($field_info){
        //取出下面数据
        $html = '<tr><th width="10%">'.$field_info['field_nikename'].'</th><td><select name="'.$field_info['field_name'].'" class="form-control">';

        //$html .= '<option value="0">请选择</option>';
        $html .= '
            <foreach name="'.$field_info['field_name'].'" item="vo">
            <option value="{$vo.id}">{$vo.title}</option>
            </foreach>
        ';
        $html .="</select>";
        if($field_info['field_tips']){
            $html .= '<p>'.$field_info['field_tips'].'</p>';
        }
        $html .="</td></tr>";
        return $html;
    }
}